﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using Xamarin.Forms;
using Capstone.Models;
using Capstone.Views;
using System.Diagnostics;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Xamarin.Forms.Xaml;

namespace Capstone.ViewModels
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public class DaysViewModel : BaseViewModel
    {
        private Item _selectedItem;
        public static string day_id = "";
        private string itemId;
        private DatePicker startDate;
        private string text;
        private string description;
        public string Id { get; set; }

        private int length;
        private string frequency;
        private string[] symptoms;
        private string[] othersymptoms;
        private string[] sleepsymptoms;
        public Item CurrentItem;
        public string CurrentId;
        public string selectedDay;


        public string ItemId
        {
            get
            {
                return itemId;
            }
            set
            {
                itemId = value;
                LoadItemId(value);
            }
        }

        public string Text
        {
            get
            {
                return text;
            }

            set
            {
                SetProperty(ref text, value);
            }
        }

        public async void LoadItemId(string itemId)
        {
            try
            {
                var item = await DataStore.GetItemAsync(itemId);
                Id = item.Id;
                Text = item.Text;
                Description = item.Description;
                Frequency = item.Frequency;
                Length = item.Length;
                Symptoms = item.Symptoms;
                OtherSymptoms = item.OtherSymptoms;
                SleepSymptoms = item.SleepSymptoms;
                startDate = item.startDate;
                CurrentItem = item;
                
            }
            catch (Exception)
            {
                Debug.WriteLine("Failed to Load Item");
            }
        }



        public int Length
        {
            get => length;
            set => SetProperty(ref length, value);
        }
        public DatePicker StartDate
        {
            get => startDate;
            set => SetProperty(ref startDate, value);
        }
        public string Day_id
        {
            get => Day_id;
            set => SetProperty(ref day_id, value);
        }
        public string Frequency
        {
            get => frequency;
            set => SetProperty(ref frequency, value);
        }

        public string[] Symptoms
        {
            get => symptoms;
            set => SetProperty(ref symptoms, value);
        }
        public string[] OtherSymptoms
        {
            get => othersymptoms;
            set => SetProperty(ref othersymptoms, value);
        }
        public string[] SleepSymptoms
        {
            get => sleepsymptoms;
            set => SetProperty(ref sleepsymptoms, value);
        }
        public string Description
        {
            get => description;
            set => SetProperty(ref description, value);
        }
        public ObservableCollection<Item> Items
        {
            get; set;
        }
        public Command LoadItemsCommand
        {
            get;
        }
        public Command AddItemCommand
        {
            get;
        }
        public Command<Item> ItemTapped
        {
            get;
        }

        public ObservableCollection<Item> days { get; set; }

        public DaysViewModel()
        {

            days = new ObservableCollection<Item>();

            Items = new ObservableCollection<Item>();

            ItemTapped = new Command<Item>(OnItemSelected);

            CurrentId = ItemsViewModel.id;
            LoadItemId(CurrentId);

            for (int i = 0; i < CurrentItem.Length; i++)
            {
                Item x = new Item();
                x.startDate = CurrentItem.startDate;
                x.Text = Properties.Resources.Day + (i+1) + "    -    " + (x.startDate).Date.AddDays(i).ToShortDateString();
                x.Id = CurrentItem.Text + "_Day_" + (i + 1) + " (ID: " + CurrentItem.Id + ")";
                x.Description = CurrentItem.Description;
                x.Frequency = CurrentItem.Frequency;
                x.Length = CurrentItem.Length;
                x.Symptoms = CurrentItem.Symptoms;
                x.OtherSymptoms = CurrentItem.OtherSymptoms;
                x.SleepSymptoms = CurrentItem.SleepSymptoms;
                string placeholder = "";
                string placeholder1 = "";
                placeholder = Preferences.Get("gut_symptom_diary_q1", "value", x.Id);
                placeholder1 = Preferences.Get("stool_q0", "value", x.Id);
                Console.WriteLine("test: " + placeholder);
                Console.WriteLine("test: " + placeholder1);
                if (Preferences.Get("finished", "value", x.Id) == "1")
                {
                    x.Progress = Color.Green;
                }
                else if (placeholder == "1" || placeholder == "0"|| placeholder1 == "1" || placeholder1 == "0")
                //if (placeholder == "1" || placeholder == "0")
                {
                    x.Progress = Color.Yellow;
                    
                }
                else
                {
                    x.Progress = Color.PaleVioletRed;
                }
                days.Add(x);


            }


        }

        async Task ExecuteLoadItemsCommand()
        {
            IsBusy = true;

            try
            {
                Items.Clear();
                var items = await DataStore.GetItemsAsync(true);
                foreach (var item in items)
                {
                    Items.Add(item);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
            finally
            {
                IsBusy = false;
            }
        }

        public void OnAppearing()
        {
            IsBusy = true;
            SelectedItem = null;
        }

        async void OnItemSelected(Item item)
        {
            string id;
            

            if (item == null)
                return;

            // This will push the ItemDetailPage onto the navigation stack

            if (item.Description == Properties.Resources.GutDescription)
            {
                id = item.Id;
                day_id = item.Id;
                //await Shell.Current.GoToAsync($"{nameof(GutSymptomPage)}?{nameof(GutSymptomViewModel.ItemId)}={item.Id}");
                await Shell.Current.GoToAsync($"{nameof(GutSymptomPage)}?{nameof(GutSymptomViewModel.ItemId)}={item.Id}");
            }
        //    else if (item.Description ==)
            if (item.Description == Properties.Resources.StoolDescription)
            {
                id = item.Id;
                day_id = item.Id;
                await Shell.Current.GoToAsync($"{nameof(StoolQualityPage)}?{nameof(StoolQualityViewModel.ItemId)}={item.Id}");
            }

            // await Shell.Current.GoToAsync($"{nameof(GutSymptomPage)}?{nameof(GutSymptomViewModel.ItemId)}={item.Id}");


        }

        public Item SelectedItem
        {
            get => _selectedItem;
            set
            {
                SetProperty(ref _selectedItem, value);
                OnItemSelected(value);
            }
        }
    }
}
